﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KZ.Express.H.ENT;
using KZ.Utilities;
using System.Data;

namespace KZ.Express.H.BLL
{
    public class ZoneBLL : BaseBLL, IBaseBLL
    {
        public override object Insert(BaseENT entity)
        {
            try
            {
                ZoneENT zone = entity as ZoneENT;

                _dal.BeginTransaction();

                zone.ValidateData();
                zone.ZoneID = Utility.IntParse(_dal.Insert(zone));

                if (zone.ZoneID == -1)
                    throw new BusinessLogicException("Mã zone đã tồn tại, bạn vui lòng nhập mã zone khác");
                else if (zone.ZoneID == -2)
                {
                    zone.IsRecovery = 1;
                    throw new BusinessLogicException("Dữ liệu có mã zone này đã bị xóa, bạn có muốn khôi phục lại?");
                }

                foreach (ZoneDetailENT zoneDetail in zone.ZoneDetails)
                {
                    zoneDetail.ZoneID = zone.ZoneID;
                    _dal.Insert(zoneDetail);
                }

                SearchCriteria search = new SearchCriteria();
                search.Add("ZoneID", zone.ZoneID);
                search.Add("ZoneCode", zone.ZoneCode);
                search.Add("Action", "Insert");
                search.Add("UserID", zone.CreatedBy);
                _dal.ExecuteNonQuery(ZoneDetailENT.InsertLogProcedure, search);

                _dal.CommitTransaction();
                return zone.ZoneID;
            }
            catch (DataAccessException ex)
            {
                _dal.RollbackTransaction();
                throw new BusinessLogicException(ex.Message);
            }
        }

        public override object Update(BaseENT entity)
        {
            try
            {
                ZoneENT zone = entity as ZoneENT;

                _dal.BeginTransaction();

                zone.ValidateData();

                SearchCriteria search = new SearchCriteria();
                search.Add("ZoneID", zone.ZoneID);
                Dictionary<string, object> content = _dal.ExecuteNonQueryOutParamDefineType(ZoneDetailENT.SelectContentProcedure, search, "Content", DbType.Xml.ToString());

                zone.ZoneID = Utility.IntParse(_dal.Update(zone));

                if (zone.ZoneID == -1)
                    throw new BusinessLogicException("Mã zone đã tồn tại, bạn vui lòng nhập mã zone khác");

                foreach (ZoneDetailENT zoneDetail in zone.ZoneDetails)
                    _dal.Insert(zoneDetail);


                search.Add("ZoneCode", zone.ZoneCode);
                search.Add("Action", "Update");
                search.Add("UserID", zone.UpdatedBy);
                search.Add("OldContent", content["Content"]);
                _dal.ExecuteNonQuery(ZoneDetailENT.InsertLogProcedure, search);

                _dal.CommitTransaction();
                return zone.ZoneID;
            }
            catch (DataAccessException ex)
            {
                _dal.RollbackTransaction();
                throw new BusinessLogicException(ex.Message);
            }
        }
    }
}
